<?php

$script = 'survey';
$page_title ='Schedule Survey';
require_once '../../controller.php';

if(isset($_REQUEST['sid'])){
	$survey_id = $_REQUEST['sid'];
 }

 
 

 if(isset($_POST['deliver_now'])){
 	header("location: deliver.php?sid=$survey_id");
 }
 
 if(isset($_POST['schedule'])){
 	
 	if(isset($_POST['set_frequency']) && $_POST['set_frequency']=='daily'){
 		$string = '';
 		if(isset($_POST['frequency1'])&& $_POST['frequency1']!=''){
 			$frequency = $_POST['frequency1'];
 			$today_date = date('Y-m-d');
 			
 			for($i=0;$i<$frequency;$i++){
 				$date[$i] = date("Y-m-d",strtotime($today_date)+($i)*24*3600);
 				
 			}
 		}elseif(isset($_POST['frequency2'])&& $_POST['frequency2']!=''){
 			$until_date = $_POST['frequency2'];
  			$unix_until_date = strtotime($until_date);
 			$today_date =date('Y-m-d');
 			$unix_today_date = strtotime($today_date);
            $i=0;
 			while($unix_today_date <$unix_until_date){
 				$date[$i]= date("Y-m-d",strtotime($today_date)+($i)*24*3600);
 				$unix_today_date = strtotime($date[$i]);
 				$i++;
 			}
 			
 		}else{
 			$error ='Enter either frequency or until date  ';
 		}
 		if(!$error){
 		$string =implode(',',$date); 
 		$string = addslashes($string);
 		$sql = "UPDATE `survey` SET `schedule`='$string',`last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update Daily Schedule'.mysql_error());
 		}
 	}
 	
 	if(isset($_POST['set_frequency']) && $_POST['set_frequency']=='weekly'){
 		
 		if(isset($_POST['select_day'])){
 			$selected_day = $_POST['select_day'];
 			for($i=0;$i<7;$i++){
 				$future_day = date('l',strtotime("+$i day"));
 				if($future_day==$selected_day){
 					$num = $i;
 					break;
 				}
 			}
 			
 			$first_date = date('Y-m-d',strtotime("+$num day"));
  		}
  			
  		if(isset($_POST['frequency1'])&& $_POST['frequency1']!=''){
 			$frequency = $_POST['frequency1'];
 			 $date[0]=$first_date;		
 			for($i=1;$i<$frequency;$i++){
 				$date[$i] = date("Y-m-d",strtotime($first_date)+($i*7)*24*3600);
  			}
 		}elseif(isset($_POST['frequency2'])&& $_POST['frequency2']!=''){
 			$until_date = $_POST['frequency2'];
  			$unix_until_date = strtotime($until_date);
 			$unix_first_date = strtotime($first_date);
            $i=0;
            $flag=true;
 			while($flag){
 				$next_date= date("Y-m-d",strtotime($first_date)+($i*7)*24*3600);
 				$unix_first_date = strtotime($next_date);
 				if($unix_first_date<$unix_until_date){
 					$date[$i]=$next_date;
  				}else{
  					$flag=false;
  				}
 				$i++;
 			}
 			
 		}else{
 			$error ='Enter either frequency or until date  ';
 		}
 		if(!$error){
 		$string =implode(',',$date); 
 		$string = addslashes($string);
 		$sql = "UPDATE `survey` SET `schedule`='$string',`last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update weekly Schedule'.mysql_error());
 		}	
 	}
 	
 	if(isset($_POST['set_frequency'])&& $_POST['set_frequency']=='monthly'){
 		
 		if(isset($_POST['select_date'])){
 			$selected_date = $_POST['select_date'];
 			if($selected_date<10){
 				$selected_date='0'.$selected_date;
 			}
 			$year = date('Y');
 			$month = date('m');
 			$every_month_date = "$year-$month-$selected_date";
 			
 		}
 		
 		if(isset($_POST['frequency1'])&& $_POST['frequency1']!=''){
 			$frequency = $_POST['frequency1'];
 			for($i=0;$i<$frequency;$i++){
 			$date[$i]= date('Y-m-d',strtotime(($every_month_date )."+$i month" ));
 			}
 		}elseif(isset($_POST['frequency2'])){
 			$until_date = $_POST['frequency2'];
 			$unix_until_date = strtotime($until_date);
 			$unix_selected_date = strtotime($every_month_date);
 			$flag=true;
 			$i=0;
 			while($flag){
 				$next_date= date("Y-m-d",strtotime(($every_month_date)."+$i month"));
 				$unix_selected_date = strtotime($next_date);
 				if($unix_selected_date<$unix_until_date){
 					$date[$i]=$next_date;
 				}else{
 					$flag=false;
 				}
 				$i++;
 			}
 		}else{
 			$error = 'Please enter either frequency or date';
 		}
 		if(!$error){
 	  $string = implode(',',$date);
 	  $string = addslashes($string);
 		$sql = "UPDATE `survey` SET `schedule`='$string',`last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update monthly Schedule'.mysql_error());
 		}
 	
 	}
 	
 	if(isset($_POST['set_frequency']) && $_POST['set_frequency']=='fixed'){
 		$i=0;
 		if(isset($_POST['date_1'])){
 			$date[$i] = $_POST['date_1'];
 		  $i++;
 		if(isset($_POST['date_2'])){
 			$date[$i] = $_POST['date_2'];
 			$i++;
 		}
 		}else{
 			$error = 'Please enter date to schedule';
 		}
 		if(!$error){
 		$string = implode(',',$date);
 	    $string = addslashes($string);
 	   
 	    $sql = "UPDATE `survey` SET `schedule`='$string', `last_sent`='' WHERE `id` = '$survey_id'";
 		$result = mysql_query($sql)or die('Update fixed date Schedule'.mysql_error());
 		}
 	}
 	
 	if(!$error){
 		header("Location:selectgroups.php?sid=$survey_id");
 	}
 	
 }
 
 require_once '../../header.php';
 if(!isset($_POST['schedule_survey'])&& !isset($_POST['schedule'])){
 	
 	echo "<form name='choose' method='post'";
 	echo "<input type='submit' name='schedule_survey' Value='Schedule for Future'<br><br>";
 	echo "OR<br><br>";
 	echo "<input type='submit' name='deliver_now' value='Deilver Now'";
 	echo "</form>";
 }
 
 if(isset($_POST['schedule_survey']) || isset($_POST['schedule'])){
 	
 	if($error){
 		echo $error;
 	}
 	echo "<table>";
 	echo "<form name='schedule_form' method='post'";
 	echo "<tr><td><input type='radio' name='set_frequency' value='daily' onclick='javascript:showFrequency();'>Daily</td></tr>";
 	echo "<tr><td><input type='radio' name='set_frequency' value='weekly' onclick='javascript:showDay();'>Weekly</td></tr>";
 	echo "<tr><td><input type='radio' name='set_frequency' value='monthly' onclick='javascript:showDate();'>Monthly</td></tr>";
 	echo "<tr><td><input type='radio' name='set_frequency' value='fixed' onclick='javascript:showFixedDate();'>Fixed Dates</td></tr>";
 	echo "<tr><td>&nbsp;</td></tr>";
 	echo "<tr><td>&nbsp;</td></tr>";
 	echo "<tr id='select_date'><td>On Date:<select name='select_date'>";
 	for($i=1;$i<32;$i++){
 		echo "<option>$i</option>";
 	}
 	echo "</select></td></tr>";
 	echo "<tr id='select_day'><td>On Every: <select name='select_day'><option>Sunday</option><option>Monday</option><option>Tuesday</option>";
 	echo "<option>Wednesday</option><option>Thursday</option><option>Friday</option><option>Saturday</option>";
 	echo "</select></td></tr>";
 	echo "<tr><td>&nbsp;</td></tr>";
 	echo "<tr id='frequency'><td>Frquency: <input type='text' name=frequency1 size = '2px'> times </td>";
 	echo "<td>     OR     </td>";
 	echo "<td>Until Date: <input type='text' name='frequency2' size = '10px'>(YYYY-MM-DD)</td></tr>";
 	echo "<tr id='date1'><td>Date 1: <input type='text' name='date_1' size='10px'></td></tr>";
 	echo "<tr id='date2'><td>Date 2: <input type='text' name='date_2' size='10px'></td></tr>";
 	echo "<tr><td>&nbsp;</td></tr>";
 	echo "<tr><td><input type='submit' name='schedule' value='schedule'></td></tr>";
 	echo "<form>";
 	echo "</table>";
 }
 
 require_once '../../footer.php';
?>